/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmConfig.h			*/
/********************************************************/

#ifndef	_CMCONFIG_H
#define	_CMCONFIG_H

#include <string>
#include "CmConf.h"
#include "CmTrans.h"
#include "CmAdxDef.h"

#define	CM_VAL_INT			0x01
#define	CM_VAL_LNG			0x02
#define	CM_VAL_FLT			0x03
#define	CM_VAL_STR			0x04
#define	CM_NUM_RDS			0x0f

#define	CM_CFG_DID			const_cast<char*>("did")
#define	CM_CFG_PORT			const_cast<char*>("port")
#define	CM_CFG_TMOUT			const_cast<char*>("timeout")
#define	CM_CFG_LOGGER			const_cast<char*>("logfile")

#define	CM_CFG_TOPIC			const_cast<char*>("topic")
#define	CM_CFG_REDIS			const_cast<char*>("redis")
#define	CM_CFG_KAFKA			const_cast<char*>("kafka")
#define	CM_CFG_TRANS			const_cast<char*>("trans")
#define	CM_CFG_GLOBAL			const_cast<char*>("global")
#define	CM_CFG_BROKER			const_cast<char*>("broker")

#define	CM_CFG_VALUES(S, K, V, T)	do {				\
	memset(szBuf, '\0', CM_BUFFER);					\
	if (CM_SUCCESS == CmConfGetVal(S, K, szBuf)) {			\
		switch(T) {						\
			case CM_VAL_INT:				\
				*V = atoi(szBuf);			\
				break;					\
			case CM_VAL_LNG:				\
				*V = atol(szBuf);			\
				break;					\
			case CM_VAL_FLT:				\
				*V = atof(szBuf);			\
				break;					\
			case CM_VAL_STR:				\
				memcpy(V, szBuf, strlen(szBuf));	\
				break;					\
			default:					\
				break;					\
		}							\
	}								\
	else {								\
		return CM_FAILURE;					\
	}								\
} while (0)

#define	CM_CFG_KVHEAD(S, K, V)						\
	if (CM_SUCCESS == CmConfGetSec(S)) {				\
		while (CM_SUCCESS == CmConfGetLine(szBuf, CM_BUFFER)) {	\
			if (true == CmConfIsValid(szBuf)) {		\
				CmConfTrim(szBuf);			\
				if (CM_SUCCESS == CmConfKeyValue(szBuf, &K, &V)) {

#define	CM_CFG_KVTAIL()							\
				}					\
			}						\
			else {						\
				break;					\
			}						\
			memset(szBuf, '\0', CM_BUFFER);			\
		}							\
	}								\
	else {								\
		return CM_FAILURE;					\
	}								\

typedef struct tagCmGlobal
{
	char	m_szDid[CM_BUFFER];
	char	m_szPort[CM_BUFFER];
	char	m_szTmOut[CM_BUFFER];
	char	m_szLogger[CM_BUFFER];
}CMGLOBAL_S;

typedef struct tagCmKafka
{
	char	m_szTopic[CM_BUFFER];
	char	m_szBroke[CM_BUFFER];
}CMKAFKA_S;

class CCmConfig : public CmCls::CCmConf
{
public:
	CCmConfig();
	virtual ~CCmConfig();

	void CmConfigPrint();
	int CmConfigLoad(char* pszFile);

	CMSET_C* CmConfigGetRedis() {return &m_CRedis;}
	CMMAP_C* CmConfigGetCTrans() {return &m_CTrans;}
	CMKAFKA_S* CmConfigGetKafka() {return &m_stKafka;}
	CMGLOBAL_S* CmConfigGetGlobal() {return &m_stGlobal;}
	CCmTrans*** CmConfigGetPTrans() {return &m_ppCTrans;}

protected:

private:
	CMSET_C		m_CRedis;
	CMMAP_C		m_CTrans;
	CMKAFKA_S	m_stKafka;
	CMGLOBAL_S	m_stGlobal;
	CCmTrans**	m_ppCTrans;

	int CmConfigKafka();
	int CmConfigRedis();
	int CmConfigCTrans();
	int CmConfigGlobal();
	void CmConfigSetDestroy();

};

#endif /* _CMCONFIG_H */
